package main

import (
	"fmt"
	"net"
)

func main() {

	// ip端口拼接与分割
	fmt.Println(net.JoinHostPort("192.268.0.1", "90"))
	fmt.Println(net.SplitHostPort("192.268.0.1:90"))

	// ip -> 主机名
	fmt.Println(net.LookupAddr("127.0.0.1"))

	// 主机名 - > ip
	fmt.Println(net.LookupHost("www.baidu.com"))
	fmt.Println(net.LookupHost("www.godufo.com"))

	// IP, IPNet
	for _, strIp := range []string{"127.0.0.1", "::1", "xxx"} {
		ip := net.ParseIP(strIp)
		fmt.Printf("%#v\n", ip)
	}

	ip, ipnet, err := net.ParseCIDR("192.168.0.1/24")
	fmt.Println(ip, ipnet, err)

	// 判断ip是否在某个网段
	fmt.Println(ipnet.Contains(net.ParseIP("192.168.0.2")))
	fmt.Println(ipnet.Contains(net.ParseIP("192.168.1.2")))

	// 获取所有接口地址
	addrs, err := net.InterfaceAddrs()
	fmt.Println(addrs, err)
	fmt.Println(addrs[0].String())

	// 获取网络接口(网卡信息)
	interfaces, _ := net.Interfaces()
	for _, intf := range interfaces {
		fmt.Println(intf.Index, intf.Name, intf.MTU, intf.HardwareAddr, intf.Flags)
		// 获取网卡地址
		fmt.Println(intf.Addrs())
	}
}
